package cc.rengu.redp.bizimpl.upmp.controller;

import cc.rengu.jrbdp.protocol.manager.ManagerTaskResult;
import cc.rengu.redp.bizimpl.igas.vo.BasePage;
import cc.rengu.redp.bizimpl.upmp.bean.BatchTaskBean;
import cc.rengu.redp.bizimpl.upmp.service.BatchTaskService;
import cc.rengu.redp.bizimpl.upmp.service.KeySynchroniseService;
import cc.rengu.redp.bizimpl.upmp.service.OneKeyStartStopService;
import cc.rengu.redp.bizimpl.utils.RbdpClient;
import cc.rengu.redp.common.domain.RedpResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api("运维监控")
@RestController
@RequestMapping("/admin/batchtask")
@ResponseBody
@SuppressWarnings({ "rawtypes", "unchecked" })
public class BatchController {

    @Autowired
    private OneKeyStartStopService oneKeyStartStopService;
    @Autowired
    private KeySynchroniseService keySynchroniseService;
    @Autowired
    private BatchTaskService batchTaskService;
    /*
     * 查询应用配置信息
     */
    @ApiOperation("运维监控:密钥同步")
    @RequestMapping(value = "/keySynchronise",method = RequestMethod.POST)
    @RequiresPermissions("keySynchronise:view")
    public RedpResponse keySynchronise(HttpServletRequest request){
        return keySynchroniseService.keySynchronise(request);
    }


    /*
     * 查询应用配置信息
     */
    @ApiOperation("运维监控:一键启停")
    @RequestMapping(value = "/getAppInfo",method = RequestMethod.GET)
    @RequiresPermissions("oneKeyStartStop:view")
    public Map<String,Object> getAppInfo(HttpServletRequest request) throws Exception {

        return oneKeyStartStopService.getAppInfo();
    }
    /*
     * 查询应用配置信息
     */
    @ApiOperation("运维监控:一键启停:启停")
    @RequestMapping(value = "/setAppInfo",method = RequestMethod.POST)
    public RedpResponse setAppInfo(HttpServletRequest request, String defaultInstId){
        // throws WebException
        RedpResponse redpResponse = new RedpResponse();
        redpResponse = oneKeyStartStopService.setAppInfo(request);

        return redpResponse;
    }

    /*
     * 获取顶级任务
     */
    @ApiOperation("运维监控:批量任务管理")
    @RequestMapping(value = "/list")
    @RequiresPermissions("batchTaskManagement:view")
    public Map<String,Object> getTopTasks(HttpServletRequest request) throws Exception {
        return batchTaskService.getTopTasks(request);
    }

    //继续任务
    @RequestMapping("/resumeTask")
    @ApiOperation("运维监控:批量任务管理")
    @RequiresPermissions("batchTaskManagement:view")
    public RedpResponse resumeTask(HttpServletRequest request){
        return batchTaskService.resumeTask(request);
    }

    /*
     * 注册
     */
    @RequestMapping("/addEntity")
    @ApiOperation("运维监控:批量任务管理")
    @RequiresPermissions("batchTaskManagement:view")
    // @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public RedpResponse addEntity(HttpServletRequest request) throws Exception {
        return batchTaskService.addEntity(request);
    }

    //重做任务
    @RequestMapping("/redoTask")
    @ApiOperation("运维监控:批量任务管理")
    @RequiresPermissions("batchTaskManagement:view")
    public RedpResponse redoTask(HttpServletRequest request){
        return batchTaskService.redoTask(request);
    }

    @RequestMapping(value = "/getAtomInfo", method = RequestMethod.GET)
    @RequiresPermissions("batchTaskManagement:view")
    @ApiOperation("运维监控:批量任务管理")
    public Map<String,Object> getAtomInfo(HttpServletRequest request) throws Exception {
        return batchTaskService.getAtomInfo(request);
    }

    /*
     *   实时任务按钮获取任务名
     */
    @GetMapping("/getTaskName")
    @RequiresPermissions("batchTaskManagement:view")
    @ApiOperation("运维监控:批量任务管理")
    public List<String> getTaskName(String dataNo) throws Exception {
        List<String> ret = RbdpClient.getIntance1().getAllTasks();
        return ret;
    }

    @PostMapping("/findSubTasks")
    @RequiresPermissions("batchTaskManagement:view")
    @ApiOperation("运维监控:批量任务管理")
    public Object findSubTasks(HttpServletRequest request) throws Exception{
        return batchTaskService.findSubTasks(request);
    }

    @PostMapping("/pauseTask")
    @RequiresPermissions("batchTaskManagement:view")
    @ApiOperation("运维监控:批量任务管理")
    public String pauseTask(HttpServletRequest request){
        return batchTaskService.pauseTask(request);
    }

    /*
     * 查询定时任务
     */
    @RequestMapping(value = "/timingTasksList")
    @ResponseBody
    public BasePage getTimingTasks(HttpServletRequest request) throws Exception {

        return batchTaskService.getTimingTasks(request);
    }
}
